﻿/* ==============================================================================
*
*   Copyright (c) 2010, Pierre Favier
*   All rights reserved.
*
*   NTasking is free software: you can redistribute it and/or modify it
*   under the terms of the GNU General Public License as published by the
*   Free Software Foundation.
*
*   Redistribution and use in source and binary forms, with or without
*   modification, are permitted provided that the following conditions
*   are met:
*
*   -   Redistributions of source code must retain the above copyright notice,
*       this list of conditions and the following disclaimer.
*
*   -   Redistributions in binary form must reproduce the above
*       copyright notice, this list of conditions and the following
*       disclaimer in the documentation and/or other materials
*       provided with the distribution.
*
*   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
*   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
*   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
*   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
*   BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
*   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
*   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
*   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
*   ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
*   TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
*   THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
*   SUCH DAMAGE.
*
* ============================================================================== */

using System;

namespace NTasking.Internals
{

    /// <summary>
    /// The <see cref="CodeWrapper"/> class is used in conjonction with the C# <c>using</c> statement
    /// to surround a code block by an action to be performed on entering the code block and
    /// an action to be performed when exiting the code block, be it on normal exit or on an
    /// exception propagation.
    /// </summary>
    internal class CodeWrapper : IDisposable
    {

        /// <summary>
        /// The action to be performed upon entering the surrounded code block
        /// </summary>
        private readonly Action _onEnter;


        /// <summary>
        /// The action to be performed upon exiting the surrounded code block
        /// </summary>
        private readonly Action _onExit;


        /// <summary>
        /// Initializes a new instance of the <see cref="CodeWrapper"/> class.
        /// </summary>
        /// <param name="onEnter">The do it.</param>
        /// <param name="onExit">The undo it.</param>
        internal CodeWrapper(Action onEnter, Action onExit)
        {
            _onEnter = onEnter;
            _onExit = onExit;
        }


        internal CodeWrapper Enter()
        {
            if (_onEnter != null)
                _onEnter();

            return this;
        }


        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            if (_onExit != null)
                _onExit();
        }
    }

}
